<?php
/**
 * Created by PhpStorm.
 * User: fengbob
 * Date: 2018/2/15
 * Time: 22:13
 */

namespace app\models;


use app\librarys\Tools;
use app\models\db\Express;
use app\models\db\Order;
use app\models\db\ZfbTradeRecord;
use yii\db\Query;

class OrderExt extends Order
{
    /**
     * 付款成功后，调用此存储过程
     * @param $order
     */
    public static function orderSuccessPrecess($record){
//        $transaction = \Yii::$app->db->beginTransaction();
      $transaction = ZfbTradeRecord::getDb()->beginTransaction();
        try {
             $record->save();
             $order = Order::findOne($record->out_trade_no);
             if(empty($order))
                 $transaction->rollBack();
             $order->status = 9;
             $order->save();
            $transaction->commit();
            $result = true;
        } catch(\Exception $e) {
            $transaction->rollBack();
            $result = false;
//            throw $e;
        } catch(\Throwable $e) {
            $transaction->rollBack();
            $result = false;
//            throw $e;
        }
        return $result;
    }
    /**
     * 数量
     * @param string $name
     * @return int|string
     */
    public static function getOrderCount($name = '')
    {
        return db\Order::find()
            ->where(['is_deleted'=>0])
            ->orFilterWhere(['like', 'receiver_name', $name])
            ->orFilterWhere(['like', 'receiver_phone', $name])
            ->count();
    }


    /**
     * 列表
     * @param int $page
     * @param int $pageSize
     * @param string $name
     * @return mixed
     */
    public static function getOrderList($page = 0, $pageSize = 20, $name = '')
    {
        if ($page <= 0) $page = 0;
        $start = $page * $pageSize;

//        $query = (new Query())
//            ->select(['t1.*','t2.'])
//            ->from('t_order as t1')
//            ->leftJoin('t_goods as t2','t1.')


        $items = db\Order::find()
            ->where(['is_deleted'=>0])
            ->orFilterWhere(['like', 'receiver_name', $name])
            ->orFilterWhere(['like', 'receiver_phone', $name])
            ->orderBy('create_datetime desc')
            ->limit($pageSize)
            ->offset($start)
            ->all();
        return $items;
    }

    /**
     *  根据id获取行
     * @param $id
     * @return array|null|\yii\db\ActiveRecord
     */
    public static function getOrderById($id)
    {
        $item = db\Order::findOne($id);

        return $item;
    }


    /**
     * save data
     * @param $id
     * @param $name
     * @param $raw_id
     * @param $app_id
     * @param $app_secret
     * @param $type
     * @param $begin_datetime
     * @param $end_datetime
     * @return bool
     */
//    public static function saveOrder($id, $name, $raw_id, $app_id, $app_secret, $type, $begin_datetime, $end_datetime, $is_enabled)
//    {
//        $item = self::getOrderById($id);
//        if (!$item) {
//            $item = new db\Order();
//            $item->id = Tools::db_uuid();
//            $item->is_deleted = 0;
//        }
//
//        $item->name = $name;
//        $item->raw_id = $raw_id;
//        $item->app_id = $app_id;
//        $item->app_secret = $app_secret;
//        $item->type = $type;
//        $item->begin_datetime = $begin_datetime;
//        $item->end_datetime = $end_datetime;
//        $item->is_enabled = $is_enabled;
//
//        $result = $item->save();
//
//        return $result;
//    }



    public static function deleteOrder($id)
    {
        $item = self::getOrderById($id);
        $item->is_deleted = 1;

        $result = $item->save();

        return $result;
    }
}